<?php

function createImage($slika, $sirina, $folder, $ime) {

	#kreiranje slicice
	$folderzaslicice = $folder;
	$sirinaslicice = $sirina;
	$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
	
	//List of allowed extensions if extlimit = yes
	$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");	
	
	//the image -> variables
	$file_type = $slika['type'];
	$file_name = $slika['name'];
	$file_size = $slika['size'];
	$file_tmp = $slika['tmp_name'];
	
	//check the file's extension
	$ext = strrchr($file_name,'.');
	$ext = strtolower($ext);
	
	//uh-oh! the file extension is not allowed!
	if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
		echo "Fajl koji ste izabrali nije slika.";
	exit();
	}
	
	//so, whats the file's extension?
	$getExt = explode ('.', $file_name);
	$file_ext = $getExt[count($getExt)-1];
	
	//create a random file name
	$izbaciti = array(' ','Š;','š','Đ','đ','Č','č','Ć','ć','Ž','ž');
	$dodati = array('-','S','s','Dj','dj','C','c','C','c','Z','z');
	$rand_name = rand(0,9999).'-'.str_replace($izbaciti, $dodati, $ime).$ext;
	
	
	//the new width variable
	$ThumbWidth = $sirinaslicice;
	/////////////////////////////////
	
	// CREATE THE THUMBNAIL //
	
	////////////////////////////////
	
	//keep image type
	if($file_size){
		
		if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
			$new_img = imagecreatefromjpeg($file_tmp);
		}
		elseif($file_type == "image/x-png" || $file_type == "image/png"){
			$new_img = imagecreatefrompng($file_tmp);
		}
		elseif($file_type == "image/gif"){
			$new_img = imagecreatefromgif($file_tmp);
		}
	
		//list the width and height and keep the height ratio.
		list($width, $height) = getimagesize($file_tmp);
		
		//calculate the image ratio
		$imgratio=$width/$height;
		$newwidth = $ThumbWidth;
		$newheight = $ThumbWidth/$imgratio;
		
		//function for resize image.
		
		$resized_img = imagecreatetruecolor($newwidth,$newheight);

		imagealphablending($resized_img, false);
		imagesavealpha($resized_img,true);
		$transparent = imagecolorallocatealpha($resized_img, 255, 255, 255, 127);
		imagefilledrectangle($resized_img, 0, 0, $newwidth, $newheight, $transparent);
		
		//the resizing is going on here!
		imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		//finally, save the image
		imagepng ($resized_img,"$folderzaslicice/$rand_name");
		imagedestroy ($resized_img);
		imagedestroy ($new_img);
		
		return($rand_name);
	}
}

function makeThumb($slika, $duzina, $visina, $folder, $ime) {
	$source_img = @imagecreatefromjpeg($slika["tmp_name"]); //Create a copy of our image for our thumbnails...
	if (!$source_img) {
		echo "could not create image handle";
		exit(0);
	}
	
	$new_w = $duzina;
	$new_h = $visina;
	
	$orig_w = imagesx($source_img);
	$orig_h = imagesy($source_img);
	
	$w_ratio = ($new_w / $orig_w);
	$h_ratio = ($new_h / $orig_h);
	
	if ($orig_w > $orig_h ) {//landscape
		$crop_w = round($orig_w * $h_ratio);
		$crop_h = $new_h;
	} elseif ($orig_w < $orig_h ) {//portrait
		$crop_h = round($orig_h * $w_ratio);
		$crop_w = $new_w;
	} else {//square
		$crop_w = $new_w;
		$crop_h = $new_h;
	}
	$dest_img = imagecreatetruecolor($new_w,$new_h);
	imagecopyresampled($dest_img, $source_img, 0 , 0 , 0, 0, $crop_w, $crop_h, $orig_w, $orig_h);
	
	
	if(imagejpeg($dest_img, $folder."/".$ime)) {
		imagedestroy($dest_img);
		imagedestroy($source_img);
	} else {
		echo "could not make thumbnail image";
		exit(0);
	}
}

	// admin
	
	function getAllLeagues() {
		$niz = array();
		$q = "SELECT id FROM liga";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Liga($n['id']);
		}
		return $niz;
	}
	
	function getAllMatches() {
		$niz = array();
		$q = "SELECT id FROM utakmica";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Match($n['id']);
		}
		return $niz;
	}
	
	function getAllTeams() {
		$niz = array();
		$q = "SELECT id FROM tim";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Tim($n['id']);
		}
		return $niz;
	}

	function getAllPlayers() {
		$niz = array();
		$q = "SELECT id FROM igrac";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Player($n['id']);
		}
		return $niz;
	}

	function getAllArticles() {
		$niz = array();
		$q = "SELECT id FROM vest";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r))
		while ($n = mysql_fetch_array($r)) {
			$niz[] = new Article($n['id']);
		}
		return $niz;
	}



?>